package com.zut.edu;

import android.os.Bundle;
import android.text.InputType;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.michaldrabik.tapbarmenulib.TapBarMenu;
import com.qmuiteam.qmui.layout.QMUILinearLayout;
import com.qmuiteam.qmui.skin.QMUISkinManager;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.qmuiteam.qmui.widget.QMUITopBarLayout;
import com.qmuiteam.qmui.widget.dialog.QMUIBottomSheet;
import com.qmuiteam.qmui.widget.dialog.QMUIDialog;
import com.qmuiteam.qmui.widget.dialog.QMUIDialogAction;
import com.zut.edu.bean.Company;
import com.zut.edu.bean.Page;
import com.zut.edu.http.BaseCallback;
import com.zut.edu.http.OkhttpHelper;
import com.zut.edu.utils.StringUtils;

import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;
import okhttp3.Response;

public class EncryptionActivity extends AppCompatActivity {
    @BindView(R.id.encryption_topbar)
    QMUITopBarLayout encryptiontopbar;
    @BindView(R.id.encryption_layout)
    QMUILinearLayout encryptionlayout;
    @BindView(R.id.encryption_message)
    TextView encryptionmessage;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_encryption);
        ButterKnife.bind(this);
        QMUIStatusBarHelper.translucent(this);
        initTopBar();
        encryptionlayout.setRadius(25);
        encryptionlayout.setAlpha(1.0f);
        encryptionlayout.setElevation(100);
        encryptionlayout.setShadowColor(getColor(R.color.qmui_config_color_blue));
        getMessage(StringUtils.BASE_URL+"/company/query");
    }
    private void initTopBar() {
        encryptiontopbar.addLeftBackImageButton().setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBackPressed();
            }
        });

        encryptiontopbar.setTitle("加密口令").setGravity(Gravity.START);
        // 切换其他情况的按钮
        encryptiontopbar.addRightTextButton("修改",R.id.topbar_right_change_button).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                EditJudge();
            }
        });


    }


    private void EditEncryption() {

        final QMUIDialog.EditTextDialogBuilder builder = new QMUIDialog.EditTextDialogBuilder(this);
        builder.setTitle("编辑")
                .setSkinManager(QMUISkinManager.defaultInstance(this))
                .setPlaceholder("在此输入新的密钥")
                .setInputType(InputType.TYPE_CLASS_NUMBER)
                .addAction("取消", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                    }
                })
                .addAction("提交", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        CharSequence text = builder.getEditText().getText();
                        if (text != null && text.length() > 3) {
                            encryptionmessage.setText(text);
                            postMessage(StringUtils.BASE_URL+"/company/update",text.toString());
                            dialog.dismiss();
                        } else {
                            Toast.makeText(EncryptionActivity.this,"输入4位以上数字",Toast.LENGTH_SHORT).show();
                        }
                    }
                })
                .create(R.style.QMUI_Dialog).show();
    }
    private void EditJudge() {
        new QMUIDialog.MessageDialogBuilder(this)
                .setMessage("修改密钥口令将影响本公司产品安全性，非本公司管理人员请取消此次操作。")
                .setSkinManager(QMUISkinManager.defaultInstance(this))
                .addAction("取消", new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                    }
                })
                .addAction(0, "修改", QMUIDialogAction.ACTION_PROP_NEGATIVE, new QMUIDialogAction.ActionListener() {
                    @Override
                    public void onClick(QMUIDialog dialog, int index) {
                        dialog.dismiss();
                        EditEncryption();
                    }
                })
                .create(R.style.QMUI_Dialog).show();
    }

    private void getMessage(String url){
        OkhttpHelper okhttpHelper = OkhttpHelper.getInstance();
        Map<String,Object>params = new HashMap<>(1);
        params.put("companyId",StringUtils.getLocalitem(this,"user","companyId"));
        okhttpHelper.get(url, params, new BaseCallback<Page<Company>>() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                Log.e("onFailure", String.valueOf(e));
            }

            @Override
            public void onSuccess(@NotNull Call call, @NotNull Response response, Page<Company> companyPage, int code) throws IOException {
                encryptionmessage.setText(companyPage.getList().get(0).getCompanyKey());
            }

            @Override
            public void onError(@NotNull Call call, @NotNull Response response, int code, Exception e) {
                Log.e("onError", String.valueOf(response));
                if(e!=null) Log.e("e", String.valueOf(e));
            }
        });
    }
    private void postMessage(String url,String message){
        OkhttpHelper okhttpHelper = OkhttpHelper.getInstance();
        Map<String,Object>params = new HashMap<>(2);
        params.put("companyKey",message);
        params.put("companyId",StringUtils.getLocalitem(this,"user","companyId"));
        okhttpHelper.post(url, params, new BaseCallback<String>() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {

            }

            @Override
            public void onSuccess(@NotNull Call call, @NotNull Response response, String s, int code) throws IOException {
                Toast.makeText(EncryptionActivity.this,"产品密钥已更新",Toast.LENGTH_SHORT).show();
            }


            @Override
            public void onError(@NotNull Call call, @NotNull Response response, int code, Exception e) {

            }
        });
    }
}
